package com.jy.adscm.core.interceptor;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author yyy
 * @ClassName DateJsonDeserializer
 * @Description TODO
 * @date 2018/11/6 14:51
 */
public class DateJsonDeserializer extends JsonDeserializer<Date> {

    public static final SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public Date deserialize(JsonParser jsonParser,DeserializationContext deserializationContext)
            throws IOException,JsonProcessingException
    {
        try
        {
            if (jsonParser != null && jsonParser.getText() != null && !"".equals(jsonParser.getText())){

                return format.parse(jsonParser.getText());
            }else {
                return null;
            }
        }
        catch(Exception e)
        {
            System.out.println("日期转换失败：" + e.getMessage());
            throw new RuntimeException(e);
        }
    }
}
